﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EC_DataBase;

namespace EasyCheque.Person
{
    public partial class AddBankAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_AddBankAccount_Click(object sender, EventArgs e)
        {
            // 先检测银行账号。
            if (OperateDB.HasRows(OperateDB.TN_PERSON_ACCOUNT, "ACCOUNT_ID", TextBox_ACCOUNT_ID.Text))
            {
                System.Diagnostics.Debug.WriteLine("Already have one.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "该银行帐户已注册。";
                return;
            }

            if (!OperateDB.HasRows(OperateDB.TN_ACCOUNT_INFO, "ACCOUNT_ID", TextBox_ACCOUNT_ID.Text))
            {
                System.Diagnostics.Debug.WriteLine("None account ID have one.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "银行帐户不存在。";
                return;
            }

            // 检测密码正确性。
            if (!Security.MD5.Exam(TextBox_ACCOUNT_PASSWORD.Text, System.Text.Encoding.ASCII, OperateDB.Get_AccountPwd(TextBox_ACCOUNT_ID.Text)))
            {
                // 检测到密码无效。
                System.Diagnostics.Debug.WriteLine("Password invalid.");
                Label_CommitMessage.Text = "密码错误。";
                return;
            }
            else
            {
                Label_CommitMessage.Text = "";
            }

            // 写数据库。
            AccessDB.ExectueSql("INSERT INTO " + OperateDB.TN_PERSON_ACCOUNT + " VALUES (" + Session["uid"] + ", '" + TextBox_ACCOUNT_ID.Text + "') ");
            AccessDB.ExectueSql("UPDATE " + OperateDB.TN_ACCOUNT_INFO + " SET PERSON = " + Session["uid"] + " WHERE ACCOUNT_ID = '" + TextBox_ACCOUNT_ID.Text + "' ");

            Session["operationDone"] = "YES";
            Response.Redirect("~/Person/Hello_Person.aspx");
        }
    }
}
